package com.thinkit.bigdata.web.thread;

import com.thinkit.bigdata.core.util.es.ESUtil;
import com.thinkit.bigdata.web.coremet.SystemStatic;
import com.thinkit.bigdata.web.model.es.audioEs.Source;
import org.apache.log4j.Logger;
import org.elasticsearch.index.query.BoolQueryBuilder;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * 规则统计多线程
 */
public class RuleStaticsThread extends Thread {

    private Logger logger = Logger.getLogger(RuleStaticsThread.class);

    private Semaphore semaphore;
    private CountDownLatch countDownLatch;

    private ESUtil esUtil;
    private int pageNum;
    private int pageSize;
    private BoolQueryBuilder queryBuilder;
    private List<Source> sourceAllList;

    public RuleStaticsThread(
            CountDownLatch countDownLatch, Semaphore semaphore, ESUtil esUtil, int pageNum, int pageSize,
            BoolQueryBuilder queryBuilder, List<Source> sourceAllList
    ) {
        this.countDownLatch = countDownLatch;
        this.semaphore = semaphore;
        this.esUtil = esUtil;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.queryBuilder = queryBuilder;
        this.sourceAllList = sourceAllList;
    }

    @Override
    public void run() {
        try {
            semaphore.acquire();
            logger.info("【规则统计】" + Thread.currentThread().getName() + " start at " + System.currentTimeMillis());

            String[] includes = new String[]
                    {
                            "serialNumber",
                            "ruleInfos.ruleId",
                            "ruleInfos.ruleName",
                            "ruleInfos.ruleTimes",
                            "ruleInfos.ruleDetails.appealUserId",
                            "ruleInfos.ruleDetails.isMissing",
                            "ruleInfos.ruleDetails.proStatus",
                            "ruleInfos.ruleDetails.matchStatus",
                            "ruleInfos.ruleDetails.processedInfos",
                            "workScores.workId",
                            "workScores.workName",
                            "workScores.modelRules.ruleId",
                            "workScores.modelRules.fateful",
                            "workScores.modelRules.useFor"
                    };
            List<String> docListSun = SystemStatic.crud.queryMapQBuilderRetRuleAndWork(
                    SystemStatic.AUDIO_ES_INDEX,
                    SystemStatic.AUDIO_ES_TYPE,
                    pageNum,
                    pageSize,
                    queryBuilder,
                    "",
                    "",
                    includes
            );
            sourceAllList.addAll(esUtil.strs2Entitys(docListSun));
            logger.info("【规则统计】" + Thread.currentThread().getName() + " stop at " + System.currentTimeMillis());
            semaphore.release();
            logger.info("【规则统计】" + Thread.currentThread().getName() + "任务结束");
            countDownLatch.countDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
